//
//  KYLoginButton.m
//  KYKJPackDemo
//
//  Created by Jordan zhou on 2017/9/28.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "KYLoginButton.h"
#import "KYLoginBgView.h"

@interface KYLoginButton()

/**
 *  渲染层
 */
@property (nonatomic,strong) CAShapeLayer *maskLayer;
/**
 *  外圈层
 */
@property (nonatomic,strong) CAShapeLayer *shapeLayer;
/**
 *  登录层
 */
@property (nonatomic,strong) CAShapeLayer *loadingLayer;
/**
 *  点击层
 */
@property (nonatomic,strong) CAShapeLayer *clickCicrleLayer;
/**
 *  登录按钮
 */
@property (nonatomic,strong) UIButton *button;
/**
 *  外部传递的frame
 */
@property (nonatomic, assign) CGRect getFrame;
/**
 *  阴影层
 */
@property (nonatomic, weak) KYLoginBgView *loginBgView;
@end

@implementation KYLoginButton

- (CAShapeLayer *)maskLayer
{
    if (_maskLayer == nil) {
        _maskLayer = [CAShapeLayer layer];
        _maskLayer.opacity = 0;
        _maskLayer.fillColor = [UIColor blueColor].CGColor;
        _maskLayer.path = [self drawBezierPath:self.frame.size.width/2].CGPath;
    }
    return _maskLayer;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUpSubLayerWithFrame:frame];
        self.getFrame = frame;
    }
    
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)decoder
{
    if (self = [super initWithCoder:decoder]) {
        
    }
    return self;
}

/**
 *  一次性初始化
 */
- (void)setUpSubLayerWithFrame:(CGRect)frame
{
    CAShapeLayer *shapeLayer = [self drawMask:frame.size.height/2];
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.strokeColor = UIColorFromRGB(0x447bf1).CGColor;
    shapeLayer.lineWidth = 2;
    [self.layer addSublayer:shapeLayer];
    [self.layer addSublayer:self.maskLayer];
    self.shapeLayer = shapeLayer;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = self.bounds;
    [button setTitle:NSLocalizedString(@"登 录", nil) forState:UIControlStateNormal];
    [button setTitleColor:UIColorFromRGB(0x447bf1) forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14.5f];
    [button addTarget:self action:@selector(clickBtn) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    self.button = button;
}

/**
 *  划线
 */
- (CAShapeLayer *)drawMask:(CGFloat)x
{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = self.bounds;
    shapeLayer.path = [self drawBezierPath:x].CGPath;
    return shapeLayer;
}

/**
 *  画贝塞尔曲线
 */
- (UIBezierPath *)drawBezierPath:(CGFloat)x
{
    CGFloat radius = self.bounds.size.height/2 - 3;
    CGFloat right = self.bounds.size.width-x;
    CGFloat left = x;
    
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    bezierPath.lineJoinStyle = kCGLineJoinRound;
    bezierPath.lineCapStyle = kCGLineCapRound;
    
    [bezierPath addArcWithCenter:CGPointMake(right, self.bounds.size.height/2) radius:radius startAngle:-M_PI/2 endAngle:M_PI/2 clockwise:YES];
    [bezierPath addArcWithCenter:CGPointMake(left, self.bounds.size.height/2) radius:radius startAngle:M_PI/2 endAngle:-M_PI/2 clockwise:YES];
    [bezierPath closePath];
    
    return bezierPath;
}

#pragma mark - 动画执行
/**
 *  点击按钮
 */
- (void)clickBtn
{
    // 0.退出键盘
    [self endEditing:YES];
    
    // 1.添加背景
    KYLoginBgView *loginBgView = [[KYLoginBgView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [loginBgView show];
    self.loginBgView = loginBgView;
    
    // 2.执行动画
    [self clickAnimation];
}

/**
 *  点击动画
 */
- (void)clickAnimation
{
    CAShapeLayer *clickCicrleLayer = [CAShapeLayer layer];
    clickCicrleLayer.position = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
    clickCicrleLayer.fillColor = UIColorFromRGB(0x447bf1).CGColor;
    clickCicrleLayer.path = [self drawclickCircleBezierPath:0].CGPath;
    [self.layer addSublayer:clickCicrleLayer];
    
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    basicAnimation.duration = 0.5;
    basicAnimation.toValue = (__bridge id _Nullable)([self drawclickCircleBezierPath:(self.bounds.size.height - 10*2)/2].CGPath);
    basicAnimation.removedOnCompletion = NO;
    basicAnimation.fillMode = kCAFillModeForwards;
    
    [clickCicrleLayer addAnimation:basicAnimation forKey:@"clickCicrleAnimation"];
    
    self.clickCicrleLayer = clickCicrleLayer;
    
    [self performSelector:@selector(clickNextAnimation) withObject:self afterDelay:basicAnimation.duration];
}

/**
 *  衔接动画
 */
- (void)clickNextAnimation
{
    self.clickCicrleLayer.fillColor = [UIColor clearColor].CGColor;
    self.clickCicrleLayer.strokeColor = UIColorFromRGB(0x447bf1).CGColor;
    self.clickCicrleLayer.lineWidth = 10;
    
    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
    
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    basicAnimation.duration = 0.15;
    basicAnimation.toValue = (__bridge id _Nullable)([self drawclickCircleBezierPath:(self.bounds.size.height - 10*2)].CGPath);
    basicAnimation.removedOnCompletion = NO;
    basicAnimation.fillMode = kCAFillModeForwards;
    
    CABasicAnimation *basicAnimation1 = [CABasicAnimation animationWithKeyPath:@"opacity"];
    basicAnimation1.beginTime = 0.10;
    basicAnimation1.duration = 0.15;
    basicAnimation1.toValue = @0;
    basicAnimation1.removedOnCompletion = NO;
    basicAnimation1.fillMode = kCAFillModeForwards;
    
    animationGroup.duration = basicAnimation1.beginTime + basicAnimation1.duration;
    animationGroup.removedOnCompletion = NO;
    animationGroup.fillMode = kCAFillModeForwards;
    animationGroup.animations = @[basicAnimation,basicAnimation1];
    
    [self.clickCicrleLayer addAnimation:animationGroup forKey:@"clickCicrleAnimation1"];
    
    [self performSelector:@selector(startMaskAnimation) withObject:self afterDelay:animationGroup.duration];
}

/**
 *  衔接动画
 */
- (void)startMaskAnimation
{
    self.maskLayer.opacity = 0.15;
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    basicAnimation.duration = 0.15;
    basicAnimation.toValue = (__bridge id _Nullable)([self drawBezierPath:self.frame.size.height/2].CGPath);
    basicAnimation.removedOnCompletion = NO;
    basicAnimation.fillMode = kCAFillModeForwards;
    [self.maskLayer addAnimation:basicAnimation forKey:@"maskAnimation"];
    [self performSelector:@selector(dismissAnimation) withObject:self afterDelay:basicAnimation.duration+0.2];
}

/**
 *  消失动画
 */
- (void)dismissAnimation
{
    [self removeSubViews];
    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
    
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    basicAnimation.duration = 0.1;
    basicAnimation.toValue = (__bridge id _Nullable)([self drawBezierPath:self.frame.size.width/2].CGPath);
    basicAnimation.removedOnCompletion = NO;
    basicAnimation.fillMode = kCAFillModeForwards;
    
    CABasicAnimation *basicAnimation1 = [CABasicAnimation animationWithKeyPath:@"opacity"];
    basicAnimation1.beginTime = 0.10;
    basicAnimation1.duration = 0.1;
    basicAnimation1.toValue = @0;
    basicAnimation1.removedOnCompletion = NO;
    basicAnimation1.fillMode = kCAFillModeForwards;
    
    animationGroup.animations = @[basicAnimation,basicAnimation1];
    animationGroup.duration = basicAnimation1.beginTime + basicAnimation1.duration;
    animationGroup.removedOnCompletion = NO;
    animationGroup.fillMode = kCAFillModeForwards;
    [self.shapeLayer addAnimation:animationGroup forKey:@"dismissAnimation"];
    
    [self performSelector:@selector(loadingAnimation) withObject:self afterDelay:animationGroup.duration];
}

/**
 *  衔接动画
 */
- (void)loadingAnimation
{
    self.loadingLayer = [CAShapeLayer layer];
    self.loadingLayer.position = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
    self.loadingLayer.fillColor = [UIColor clearColor].CGColor;
    self.loadingLayer.strokeColor = UIColorFromRGB(0x447bf1).CGColor;
    self.loadingLayer.lineWidth = 2;
    self.loadingLayer.path = [self drawLoadingBezierPath].CGPath;
    [self.layer addSublayer:self.loadingLayer];
    
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    basicAnimation.fromValue = @(0);
    basicAnimation.toValue = @(M_PI*2);
    basicAnimation.duration = 0.5;
    basicAnimation.repeatCount = LONG_MAX;
    [self.loadingLayer addAnimation:basicAnimation forKey:@"loadingAnimation"];
    [self performSelector:@selector(removeAllAnimation) withObject:self afterDelay:1.5];
}

#pragma mark - 移除动画
- (void)removeSubViews
{
    self.maskLayer.opacity = 0;
    [self.button removeFromSuperview];
    [self.maskLayer removeFromSuperlayer];
    [self.loadingLayer removeFromSuperlayer];
    [self.clickCicrleLayer removeFromSuperlayer];
}

- (void)removeAllAnimation
{
    [self removeSubViews];
    
    [self setUpSubLayerWithFrame:self.getFrame];
    
    [self.loginBgView hide];
    
    if (self.translateBlock) {
        self.translateBlock();
    }
}

#pragma mark - 画贝塞尔曲线
- (UIBezierPath *)drawclickCircleBezierPath:(CGFloat)radius
{
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    [bezierPath addArcWithCenter:CGPointMake(0,0) radius:radius startAngle:0 endAngle:M_PI*2 clockwise:YES];
    return bezierPath;
}

- (UIBezierPath *)drawLoadingBezierPath
{
    CGFloat radius = self.bounds.size.height/2 - 3;
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    [bezierPath addArcWithCenter:CGPointMake(0,0) radius:radius startAngle:M_PI/2 endAngle:M_PI/2+M_PI/2 clockwise:YES];
    return bezierPath;
}


@end
